{extend name='../app/admin/view/common/pop.html' /}
{block name="content"}
<form class="layui-form" style="margin-top:15px;width:80%;" id="form">
    <div class="layui-form-item">
        <label class="layui-form-label">上级分类</label>
        <div class="layui-input-block ">
            <input id="tree" type="text" placeholder="请选择" value="" readonly="readonly" class="layui-input">
            <input type="hidden" name="pid" value="0">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-form-label"></div>
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-normal" lay-submit  lay-filter="create">提交</button>
        </div>
    </div>
</form>
{/block}


{block name="script"}
<script>
    layui.config({
        base: '/static/extra/layui/plugins/treeselect/module/'
    }).extend({
        treeSelect: 'treeSelect/treeSelect'
    }).use(['treeSelect', 'form', 'layer'], function () {
        var treeSelect = layui.treeSelect,
            form = layui.form,
            layer = layui.layer;
        $ = layui.jquery;
        treeSelect.render({
            elem: '#tree',
            data: '{:plugin_url("getSelectTree")}',
            // 请求头
            headers: {},
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '请选择菜单',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 一些可定制的样式
            style: {
                folder: {
                    enable: false
                },
                line: { // 连接线
                    enable: true // 是否开启：true/false
                }
            },
            // 点击回调
            click: function(d){
                console.log(d);
                $('input[name=pid]').val(d.current.id);
            },
            // 加载完成后的回调函数
            success: function (d) {
                treeSelect.refresh('tree');
            }
        });


        form.on('submit(create)',function(data){
            var loading,
                fd = new FormData(document.getElementById("form"));
            $.ajax({
                url:"{:plugin_url('',['id'=>$info['id']])}",
                type:'post',
                processData: false,//必须
                contentType: false,//必须
                data:fd,
                beforeSend:function(){
                    loading = layer.load();
                },
                complete:function(){
                    layer.close(loading);
                },
                success:function(r){
                    layer.msg(r.msg);
                    if (r.code == 1) {
                        setTimeout(function(){
                            parent.location.reload();
                        },1000)
                    }
                },
                error:function(r){
                    layer.msg('系统异常');
                    console.log(r);
                }
            });
            return false;
        });

    })
</script>
{/block}
